<template>
  <div class="machine-repair">
    <div>
      <label>{{ $t("route.Maintenance_Management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Maintenance_worker_maintenance_record") }}</label>
      </button>
    </div>

    <div>
      <div>
        <el-date-picker @change="pickerChange" v-model="dataMonth" type="month" />
      </div>
      <div>
        <div>
          <div v-if="chartShow" id="echart-1" style="height: 250px; width: 100%"></div>
        </div>
        <div>
          <div v-if="chartShow" id="echart-2" style="height: 250px; width: 100%"></div>
        </div>
        <div>
          <div v-if="chartShow" id="echart-3" style="height: 250px; width: 100%"></div>
        </div>
      </div>
    </div>

    <div>
      <div>
        <div class="div-handle-right">
          <el-button @click="searchClick">{{ $t("route.search") }}</el-button>
          <input v-model="query.search" :placeholder="$t('route.Name_of_mechanic')" />
        </div>
      </div>

      <el-table class="table" :data="repairList" style="width: 100%">
        <el-table-column prop="nickname" :label="$t('route.Maintenance_technician')" />
        <el-table-column prop="kind" :label="$t('route.kind')" />
        <el-table-column prop="noseCode" :label="$t('route.Head_Model')" />
        <el-table-column
          prop="problem"
          :label="$t('route.Maintenance_issues')"
          width="200"
        />
        <el-table-column :label="$t('route.Repair_time')">
          <template #default="scope">
            {{ new Date(scope.row.createTime).format("yyyy-MM-dd hh:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Maintenance_time')">
          <template #default="scope">
            {{
              scope.row.repairTime
                ? new Date(scope.row.repairTime).format("yyyy-MM-dd hh:mm:ss")
                : ""
            }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Waiting_time')">
          <template #default="scope">
            {{ formatWaitDur(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Maintenance_duration')">
          <template #default="scope">
            {{ formatRepairDur(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Total_time')">
          <template #default="scope">
            {{ formatAllDur(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column width="80" :label="$t('route.Processing_results')">
          <template #default="scope">
            <label
              :style="{
                color:
                  formatResult(scope.row.result) == $t('route.normal')
                    ? '#444444'
                    : '#FF0000',
              }"
              >{{ formatResult(scope.row.result) }}</label
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import * as echarts from "echarts";
import Constant from "../../js/common/constant.js";
import i18n from "../../i18n/index";
import { markRaw } from "vue";

export default {
  data() {
    return {
      chartShow: false,
      dataMonth: new Date(),
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        search: null,
      },
      repairList: [],
    };
  },
  methods: {
    formatAllDur(repair) {
      var dur = 0;
      if (repair.finishTime) {
        dur = repair.finishTime - repair.createTime;
      } else {
        dur = new Date().getTime() - repair.createTime;
      }
      return CommonUtils.timeStamp(dur / 1000);
    },

    formatWaitDur(repair) {
      if (repair.repairTime) {
        var dur = repair.repairTime - repair.createTime;
        return CommonUtils.timeStamp(dur / 1000);
      }
    },
    formatRepairDur(repair) {
      if (repair.finishTime) {
        var dur = repair.finishTime - repair.repairTime;
        return CommonUtils.timeStamp(dur / 1000);
      }
    },
    pickerChange() {
      this.getUserRepairReport();
    },
    getUserRepairReport() {
      http.post(
        api.GET_USER_REPAIR_REPORT,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,

          date: this.dataMonth.format("yyyy-MM"),
        },
        (data) => {
          var xData = [];
          var y1Data = [];
          var y2Data = [];
          for (var item of data.data) {
            xData.push(item.nickname);
            y1Data.push(item.num);
            y2Data.push(item.num ? (item.rateSum / item.num).toFixed(2) : 0);
          }
          this.initEchart1(xData, y1Data);
          this.initEchart2(xData, y2Data);
        }
      );
    },
    getUserRepairReport1() {
      http.post(
        api.GET_USER_REPAIR_REPORT1,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          date: this.dataMonth.format("yyyy-MM"),
        },
        (data) => {
          var xData = [];
          var yData = [];
          for (var item of data.data) {
            xData.push(item.nickname);
            yData.push(item.allDur ? (item.allDur / 3600).toFixed(2) : 0);
          }
          this.initEchart3(xData, yData);
        }
      );
    },
    getRepairList() {
      http.post(
        api.GET_REPAIR_LIST5,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          search: this.query.search,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
        },
        (data) => {
          this.repairList = data.data.repairList;
          this.query.total = data.data.total;
        }
      );
    },

    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getRepairList();
    },
    formatDur(dur) {
      if (!dur) return "---";
      return CommonUtils.formatDur(dur);
    },

    initEchart1(xData, yData) {
      var myChart = markRaw(echarts.init(document.getElementById("echart-1")));
      var option = CommonUtils.initBarOption(
        i18n.global.t("route.Maintenance_times"),
        xData,
        yData
      );
      option.yAxis.minInterval = 1;
      option.dataZoom = [
        {
          type: "inside",
        },
      ];
      myChart.setOption(option);
    },

    initEchart2(xData, yData) {
      var myChart = markRaw(echarts.init(document.getElementById("echart-2")));
      var option = CommonUtils.initBarOption(
        i18n.global.t("route.Favorable_rate"),
        xData,
        yData
      );
      option.dataZoom = [
        {
          type: "inside",
        },
      ];
      myChart.setOption(option);
    },
    initEchart3(xData, yData) {
      var myChart = markRaw(echarts.init(document.getElementById("echart-3")));
      var option = CommonUtils.initBarOption(
        i18n.global.t("route.Maintenance_duration"),
        xData,
        yData
      );
      option.dataZoom = [
        {
          type: "inside",
        },
      ];
      myChart.setOption(option);
    },
    formatResult(result) {
      //0:没有异常 1:普通维修 2:大修 3报废 4:异常报警
      if (result == 0) {
        return i18n.global.t("route.normal");
      } else if (result == 1) {
        return i18n.global.t("route.General_maintenance");
      } else if (result == 2) {
        return i18n.global.t("route.Overhaul");
      } else if (result == 3) {
        return i18n.global.t("route.Scrap");
      } else if (result == 4) {
        return i18n.global.t("route.Abnormal_alarm");
      }
    },
    searchClick() {
      this.query.pageNum = 1;
      this.getRepairList();
    },
  },
  created() {
    this.query.search = this.$route.params.search;
    this.chartShow = true;
    this.getUserRepairReport();
    this.getUserRepairReport1();
    this.getRepairList();
  },
  destroyed() {
    this.chartShow = false;
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
.machine-repair {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div:not(:nth-child(2n)) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  .select {
    width: 120px;
    margin-left: 10px;
  }

  > div:nth-of-type(1) {
    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    margin-top: 20px;

    > div:nth-of-type(1) {
      background-color: #ffffff;
      padding: 10px;
      margin-bottom: 10px;
    }

    > div:nth-of-type(2) {
      display: flex;
      flex-direction: row;
      align-items: center;

      > div {
        background-color: #ffffff;
        padding: 20px;
        border-radius: 5px;
        flex: 1;
      }

      > div:nth-of-type(1) {
        // margin-right: 10px;
      }

      > div:nth-of-type(2) {
        margin-left: 10px;
      }

      > div:nth-of-type(3) {
        margin-left: 10px;
      }
    }
  }

  > div:nth-of-type(3) {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-top: 20px;
    background-color: #ffffff;

    > div {
      width: 100%;
    }

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }
  }
}
</style>
